/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.data.value.MLNumberValue;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.tools.ScrolledRangeModel;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Marek Brun
	 */
	public class ScrolledMC extends ScrolledRangeModel implements IMLBroadcasterListener {

		private var mlmc:MLMC;
		public var mcHeight:MLNumberValue;
		private var mcMask:DisplayObject;
		private var isYAxis:Boolean;
		
		public function ScrolledMC(mc:MovieClip, mcMask:DisplayObject, isYAxis:Boolean=true, isEnableAutoMCHeight:Boolean=true, mcHeight:MLNumberValue=null) {
			mlmc=MLMC.forInstance(mc);
			this.isYAxis=isYAxis;
			this.mcMask=mcMask;
			
			
			this.mcHeight=mcHeight==null ? new MLNumberValue() : mcHeight;
			this.mcHeight.v=100;
			this.mcHeight.addListener(this);
			addListener(this);
			
			setupScrolledLength(this.mcHeight.v);
			
			if(isEnableAutoMCHeight){
				enableAutoMCHeight();
			}
			
			draw();
		}
		
		public function enableGlobalMouseWhell():void {
			mc.stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMaskMouseWhell);
		}
		
		public function enableAutoMCHeight():void {
			mc.addEventListener(Event.ENTER_FRAME, onMCEnterFrame, false, 0, true);
		}
		
		public function draw():void {
			if(isYAxis){
				setupScrollArea(mcMask.y, mcMask.y+mcMask.height);
				mc.y=Math.min(mcMask.y, getScrolledBegin());
			}else{
				setupScrollArea(mcMask.x, mcMask.x+mcMask.width);
				mc.x=getScrolledBegin();
			}
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
//********************************************************************************************
//		events for ScrolledMC
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case mcHeight:
					switch(ev.event) {
						case mcHeight.event_Changed:
							setupScrolledLength(mcHeight.v);
						break;
					}
				break;
				case this:
					switch(ev.event) {
						case this.event_ScrollChanged:
							draw();
						break;
					}
				break;
			}
		}
		
		protected function onMCEnterFrame(event:Event):void {
			if(isYAxis){
				mcHeight.v=mc.height;
			}else{
				mcHeight.v=mc.width;
			}
		}
		
		protected function onMaskMouseWhell(event:MouseEvent):void {
			setScrollByOneUnit(event.delta<0);
		}
	}
}
